var api = require('API');
module.exports = ['$scope', function RoleController($scope) {
    $scope.list_block_data = [];

    //获取用户信息
    (function role_user_data() {
        api.user.brief(function (data) {
            $scope.$apply(function () {
                console.log(data);
                $scope.user_data = data;
            })
        });
    })();

    //获取所有角色列表
    function refresh(cb) {
        api.rbac.roles(function (data) {
            $scope.$apply(function () {
                $scope.roles_all_data = data;
                if (cb)cb();
            })
        });
    }

    //用户已有角色
    function roles_user(data) {
        api.rbac.roles_for_user(data, function (json) {
            $scope.$apply(function () {
                $scope.role_data = json;
            })
        });
    }

    //用户list颜色管理
    $scope.user_list = function (list, data) {

        if (list.iscolor) {
            list.iscolor = false;
        } else {
            for (var i = 0; i < data.length; i++) {
                $scope.user_data[i].iscolor = false;
            }
            list.iscolor = true;
        }
        $scope.list_block_data = list;

        if ($scope.list_block_data == '') {
            alert('没数据');
        } else {
            api.rbac.roles_for_user($scope.list_block_data.id, function (json) {
                refresh(function () {
                    $scope.role_data = json;
                    for (var i = 0; i < $scope.roles_all_data.length; i++) {
                        for (var j = 0; j < $scope.role_data.length; j++) {
                            if ($scope.roles_all_data[i].id === $scope.role_data[j].id) {
                                $scope.roles_all_data.splice(i, 1);
                            }
                        }
                    }

                });
            });

        }

    };

    //增加一个角色
    $scope.role_list = function (list) {
        var user = $scope.list_block_data.id;
        var role = list.id;
        console.log('user',user,'add-role',list.id);
        api.rbac.add_role_for_user(user, role, function () {
            api.rbac.roles_for_user(user, function (json) {
                $scope.$apply(function () {
                    refresh(function () {
                        $scope.role_data = json;
                        for (var i = 0; i < $scope.roles_all_data.length; i++) {
                            for (var j = 0; j < $scope.role_data.length; j++) {
                                if ($scope.roles_all_data[i].id === $scope.role_data[j].id) {
                                    $scope.roles_all_data.splice(i, 1);
                                }
                            }
                        }
                        list.is_role_color = true;
                    });
                })
            });
        });
    };

    //删除一个角色
    $scope.role_del = function (list) {
        console.log(list);
        var user = $scope.list_block_data.id;
        api.rbac.remove_role_for_user(user, list.id, function (data) {
            api.rbac.roles_for_user(user, function (json) {
                refresh(function () {
                    $scope.role_data = json;
                    for (var i = 0; i < $scope.roles_all_data.length; i++) {
                        for (var j = 0; j < $scope.role_data.length; j++) {
                            if ($scope.roles_all_data[i].id === $scope.role_data[j].id) {
                                $scope.roles_all_data.splice(i, 1);
                            }
                        }
                    }
                });
            });
        });
    }

}];
